package com.linln.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class DateUtils {

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYY_MM_DD_X = "yyyy/MM/dd";

    public static String format(Date date, String pattern) {
        if(date != null){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    public static String getDateByLocalTime(LocalDateTime time) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return df.format(time);
    }

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static String getDates()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static String getHour()
    {
        return dateTimeNow("HH");
    }

    public static String getDay()
    {
        return dateTimeNow("d");
    }
    public static String getMonth()
    {
        return dateTimeNow("MM");
    }
    public static String getYear()
    {
        return dateTimeNow("yyyy");
    }

    public static String getTempTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM);
    }

    public static String dateTimeNow(String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static String parseDateToStr(String format, Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 将字符串格式日期按设定的格式解析为日期
     * @param format
     * @param ts
     * @return
     */
    public static Date parseStrToDate(String format, String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 比较两个日期大小
     * @param date1
     * @param date2
     * @return
     */
    public static int compare(Date date1,Date date2){
        int param = 0;
        if (date1.compareTo(date2) > 0) {
            param = 1;
        } else if (date1.compareTo(date2) < 0) {
            param = -1;
        } else {
            param = 0;
        }
        return param;
    }

    /**
     * 获取日期对应星期几
     * @param date
     * @return
     */
    public static int getWeek(Date date){
        int[] weeks = {7,1,2,3,4,5,6};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weeks[w];
    }

    /**
     * 计算两个日期相差多少天
     * @param startdate
     * @param enddate
     * @return
     * @throws ParseException
     */
    public static int daysBetween(String startdate,String enddate) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(startdate));
        long start = calendar.getTimeInMillis();
        calendar.setTime(sdf.parse(enddate));
        long end = calendar.getTimeInMillis();
        long betweendays=(end-start)/(1000*3600*24);
        int days = Integer.parseInt(String.valueOf(betweendays));
        return days;
    }

    /**
     * 计算两个日期相差多少分钟
     * @param startdate
     * @param enddate
     * @return
     * @throws ParseException
     */
    public static int minuteBetween(String startdate,String enddate) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(startdate));
        long start = calendar.getTimeInMillis();
        calendar.setTime(sdf.parse(enddate));
        long end = calendar.getTimeInMillis();
        long betweenminutes=(end-start)/(1000*60);
        int minutes = Integer.parseInt(String.valueOf(betweenminutes));
        return minutes;
    }


    /**
     *获取单个的时间节点
     * @param date
     * @return 0 年 1月 2日 3时 4分 5秒 6该月的天数
     */
    public static Map<Integer,Integer> getSingleTimes(Date date){
        Map<Integer,Integer> map = new HashMap<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //年份
        int year = cal.get(Calendar.YEAR);
        //月份
        int month = cal.get(Calendar.MONTH)+1;
        //日
        int day = cal.get(Calendar.DAY_OF_MONTH);
        //时
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        //分
        int min = cal.get(Calendar.MINUTE);
        //秒
        int second = cal.get(Calendar.SECOND);
        // 设置日历对象为指定年月日，为指定月份的第一天
        cal.set(Calendar.DATE, 1);//把日期设置为当月第一天
        cal.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        //该月的天数
        int month_day = cal.get(Calendar.DATE);
        map.put(0,year);
        map.put(1,month);
        map.put(2,day);
        map.put(3,hour);
        map.put(4,min);
        map.put(5,second);
        map.put(6,month_day);
        return map;
    }

    /**
     * 获取距离今天几天的日期
     * @param num
     * @return
     */
    public static String getOneDay(int num){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();

        calendar.setTime(new Date());

        calendar.add(calendar.DATE,num);

        String date2= sdf.format(calendar.getTime());
        return  date2;
    }

    /**
     * 时间戳转为日期
     * @param timeStamp
     * @return
     */
    public static String parseTimeStampToStr(String format,long timeStamp){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        //转换
        String time = sdf.format(new Date(timeStamp));
        return time;
    }

    /**
     * 在日期上面新增天数
     * @param dt
     * @param day
     * @return
     */
    public static long addDays(Date dt,int day){
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        c.add(Calendar.DATE, day);
        Date time = c.getTime();
        return time.getTime();
    }

    // expireDate - nowDate 的天数差（返回正数表示剩余天数）
    public static long getDaysBetween(Date startDate, Date endDate) {
        long diff = endDate.getTime() - startDate.getTime();
        return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
    }
}
